'use strict';
const {getDefaultModelConfig} = require("../../utils/common");
const {formatDecimal} = require("../../utils");

module.exports = app => {
    const { STRING, INTEGER, TINYINT, DECIMAL, TEXT } = app.Sequelize;
    const BalanceRecord = app.model.define(
        'BalanceRecord',
        {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            order_no: STRING, // 订单号
            out_bill_no: STRING, // 提现单号
            transfer_bill_no: STRING, // 微信转账单号
            state: STRING, // 状态
            fail_reason: STRING,
            travel_share_order_id: INTEGER, // 分销订单ID
            travel_order_id: INTEGER, // 旅行订单ID
            user_id: {
                type: INTEGER,
                comment: '用户ID'
            },
            from_user_id: {
                type: INTEGER,
                comment: '来源用户ID'
            },
            amount: {
                type: DECIMAL(10, 2),
                comment: '金额，正数表示收入，负数表示提现',
                get() {
                    return formatDecimal(this.getDataValue('amount'))
                }
            },
            amount2: {
                type: DECIMAL(10, 2),
                comment: '手续费',
                get() {
                    return formatDecimal(this.getDataValue('amount2'))
                }
            },
            type: {
                type: TINYINT,
                comment: '1 提现 2 佣金收入 3 订单扣减 4 订单退款 5充值'
            },
            description: {
                type: TEXT,
                comment: '描述'
            },
            status: {
                type: TINYINT,
                comment: '0 待处理 1 成功 2 失败'
            },
            ...getDefaultModelConfig(app)
        },
        {
            tableName: 'balance_record',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );
    BalanceRecord.associate = function () {
        app.model.Business.BalanceRecord.belongsTo(app.model.Business.User, {
            foreignKey: 'user_id',
            targetKey: 'id',
            as: 'user'
        });
    };
    return BalanceRecord
};
